package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiNilLoader.Array;

import db.CrashBean;
import db.DataBase;

public class DisplayCrashServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public DisplayCrashServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out
				.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><meta http-equiv='Content-Type' content='text/html; charset=utf-8'></HEAD>");
		out.println("  <BODY>");
		out.println("crash上报");
		out.println("  <table border='1'><tr><th>时间</th><th>机型</th><th>系统版本</th><th>app包名</th><th>app版本号</th>" +
				"<th>crash信息</th></tr>");
		
	
		Map<String,CrashBean> tem = DataBase.getAllCrash();
		;
		
		List list = Arrays.asList(tem.keySet().toArray());
		java.util.Collections.sort(list);
		System.out.println(list);
		for(int i=(list.size()-1);i>=0;i--){
			String key = (String)list.get(i);
			CrashBean bean = tem.get(key);
			key = key.replace("T", " ");
			key = key.replace(".000+08:00", "");
			out.println("<tr><td>"+key+"</td><td>"+bean.getPHONE_MODEL()+"</td><td>"+bean.getANDROID_VERSION()+"</td>" +
					"<td>"+bean.getPACKAGE_NAME()+"</td><td>"+bean.getAPP_VERSION_NAME()+"</td>" +
							"<td>"+bean.getSTACK_TRACE()+"</td></tr>");
			
			
		}

		out.println("</table>");
	
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out
				.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the POST method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
